package pl.pkolodziej.utils;

import org.springframework.security.core.codec.Base64;

public class XORcryptor {
	
	public static int key = 129;

    public static String encrypt(String textToEncrypt)
    {            
    	char[] inArr = textToEncrypt.toCharArray();
    	char[] outArr = new char[textToEncrypt.length()];
        char c;
        for (int i = 0; i < textToEncrypt.length(); i++)
        {
            c = inArr[i];
            c = (char)(c ^ key);
            outArr[i] = c;
        }
        byte[] bArr = outArr.toString().getBytes();
        return Base64.encode(bArr).toString();
    }   
    
    public static String decrypt(String text)
    {           
    	byte[] bArr = text.getBytes();
        String textToEncrypt =  Base64.decode(bArr).toString();
        
    	char[] inArr = textToEncrypt.toCharArray();
    	char[] outArr = new char[textToEncrypt.length()];
        char c;
        for (int i = 0; i < textToEncrypt.length(); i++)
        {
            c = inArr[i];
            c = (char)(c ^ key);
            outArr[i] = c;
        }
        return outArr.toString();
    }   
    
    public static void main(String[] args){
    	System.out.println("::"+encrypt("AAAA"));
    	System.out.println("::"+decrypt("[B@de6ced"));
    	
    }
	
}
